/**
 * 
 */
package org.swing.utility.jai.magic;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.swing.utility.jai.bean.ImageSize;
import org.swing.utility.jai.imp.ImageAction;

/**
 * @author Quynh Nhu
 * 
 */
public class ImageMagickCmd implements ImageAction {
	private static final Logger log = Logger.getLogger(ImageMagickCmd.class);

	public void scale(final File srcFile, final File destFile, final int width,
			final int height) {
		List command = new ArrayList();
		int size = Math.max(width, height);
		command.add("convert");
		command.add("-size");
		command.add(size + "x" + size);
		command.add(srcFile.getAbsolutePath());
		command.add("-resize");
		command.add(size + "x" + size);
		command.add("-quality");
		command.add(String.valueOf(75));
		command.add(destFile.getAbsolutePath());

		destFile.getParentFile().mkdirs();
		exec((String[]) command.toArray(new String[1]));
	}

	public void scale(final File srcFile, final File destFile,
			final ImageSize size) {
		scale(srcFile, destFile, size.getWidth(), size.getHeight());
	}

	public void scale(final File srcFile, final List destFiles, final List sizes) {
		try {
			Iterator fileIter = destFiles.iterator();
			Iterator sizeIter = sizes.iterator();
			while (sizeIter.hasNext() && fileIter.hasNext()) {
				ImageSize size = (ImageSize) sizeIter.next();
				File destFile = (File) fileIter.next();
				scale(srcFile, destFile, size.getWidth(), size.getHeight());
			}
		} catch (Throwable e) {
			log.debug(e.getMessage(), e);
		}
	}

	/**
	 * Tries to exec the command, waits for it to finsih, logs errors if exit
	 * status is nonzero, and returns true if exit status is 0 (success).
	 * 
	 * @param command
	 *            Description of the Parameter
	 * @return Description of the Return Value
	 */
	private static boolean exec(final String[] command) {
		Process proc;

		try {
			proc = Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			log.error("IOException while trying to execute " + command);
			return false;
		}

		int exitStatus;

		while (true) {
			try {
				exitStatus = proc.waitFor();
				break;
			} catch (java.lang.InterruptedException e) {
				log.error("Interrupted: Ignoring and waiting");
			}
		}
		if (exitStatus != 0) {
			log.error("Error executing command: " + exitStatus);
		}
		return (exitStatus == 0);
	}

	public void thumbnail(final File srcFile, final File destFile,
			final int width, final int height) {
		List command = new ArrayList();
		int size = Math.max(width, height);
		command.add("convert");
		command.add("-size");
		command.add(size + "x" + size);
		command.add(srcFile.getAbsolutePath());
		command.add("-thumbnail");
		command.add(size + "x" + size);
		command.add("-quality");
		command.add(String.valueOf(50));
		command.add(destFile.getAbsolutePath());

		destFile.getParentFile().mkdirs();
		exec((String[]) command.toArray(new String[1]));
	}

	public void thumbnail(final File srcFile, final File destFile,
			final ImageSize thumbnailSize) {
		thumbnail(srcFile, destFile, thumbnailSize.getWidth(),
				thumbnailSize.getHeight());

	}

}
